package com.xzy.urp.handle;

import com.xzy.urp.common.exception.BusinessException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 使用内存标记判断是否开启选课
 */
public class TimingHandle extends HandlerInterceptorAdapter {

    //内存标记
   private volatile boolean flag ;

    //单例模式
    private volatile static TimingHandle timingHandle;

    private TimingHandle(){}

    private TimingHandle( boolean flag) {
        this.flag = flag;
    }

    public  static TimingHandle getTimingHandle(){
        if(null == timingHandle){
            synchronized(TimingHandle.class){
                if(null == timingHandle){
                    timingHandle = new TimingHandle(false);
                    timingHandle.over();
                }
            }
        }
        return timingHandle;
    }
    public void start(){
        flag = true;
    };
    public  void over(){
        flag = false;
    };
    //拦截器执行方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        // 不需要进行注解判断，可以直接使用url前缀进行拦截


        if(!flag){
            throw new BusinessException("非选课阶段",-211);
        }


        return this.flag;
    }

}
